package com.yunxi.service.localService.impl.sto;

import com.github.pagehelper.PageInfo;
import com.yunxi.core.page.PageHelperPlus;
import com.yunxi.core.service.ServiceImplPlus;
import com.yunxi.core.web.TreeConfig;
import com.yunxi.dao.sto.StoManageMapper;
import com.yunxi.model.sto.StoManage;
import com.yunxi.service.localService.sto.StoManageService;
import com.yunxi.service.localService.sto.VehicleStockMService;
import com.yunxi.sto.pojo.StoManageEditVO;
import com.yunxi.sto.pojo.StoManageVO;
import com.yunxi.sto.pojo.VehicleStockFrozenListVO;
import com.yunxi.sto.pojo.VehicleStockFrozenVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 仓库结构管理 服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-05-14
 */
@Service
public class StoManageServiceImpl extends ServiceImplPlus<StoManageMapper, StoManage> implements StoManageService {

    @Autowired
    @Qualifier(value = "vehicleStockMServiceImpl")
    VehicleStockMService vehicleStockService;

    @Override
    public void delAndReset(List<String> list,Integer isDel,String userId) {
        if(list != null && list.size() > 0){
            for (String storageId:list){
                StoManage stoManage = baseMapper.selectById(storageId);
                stoManage.setStorageId(storageId);
                stoManage.setIsPositionDisabled(isDel);
                stoManage.setUpdateBy(userId);
                baseMapper.updateById(stoManage);
            }
        }
    }

    /**
     * 作废,所有下级全部同时作废,排除所有已占用,已冻结的库位
     * @param list
     */
    @Override
    public void disabled(List<String> list,String userId) throws Exception{
        if(list != null && list.size() > 0){
            for (String storageId:list){
                StoManage stoManage = baseMapper.selectById(storageId);
                if(stoManage != null){
                    // 库位使用状态 占用dict_847,空闲 dict_846
                    String storagePositionState = stoManage.getStoragePositionState();
                    //库位是否冻结(1是0否)
                    Integer isPositionFrozen = stoManage.getIsPositionFrozen();
                    if((storagePositionState == null || storagePositionState.equals("1"))
                            &&(isPositionFrozen == null || isPositionFrozen.equals(0))){
                        baseMapper.disabled(storageId);
                    }
                    else{
                        throw new Exception("操作失败,已冻结或者已占用库位不可以作废!");
                    }
                    /*stoManage.setUpdateBy(userId);
                    baseMapper.updateById(stoManage);*/
                }
            }
        }
    }

    /**
     * 库位冻结/解冻
     * @param list
     * @param isFrozen
     */
    @Override
    public void frozenAndReset(List<VehicleStockFrozenVO> list, Integer isFrozen, String userId)throws Exception {
        if(list != null && list.size() > 0){
            for (VehicleStockFrozenVO vo:list){
                String storageId = vo.getBusinessKey();
                StoManage stoManage = baseMapper.selectById(storageId);
                if(isFrozen == 1){
                    stoManage.setIsManualFrozen(1);
                    //只能冻结空闲,未作废的库位
                    String storagePositionState = stoManage.getStoragePositionState();
                    Integer isPositionDisabled = stoManage.getIsPositionDisabled();
                    Integer isPositionFrozen = stoManage.getIsPositionFrozen();
                    if((storagePositionState != null && storagePositionState.equals("2")) ||(isPositionDisabled != null && isPositionDisabled.equals(1)) || (isPositionFrozen != null && isPositionFrozen.equals(1))){
                        throw new Exception("操作失败,只能冻结空闲,未作废,未冻结的库位!");
                    }
                }
                else if(isFrozen == 0){
                    //只能解冻手动冻结的库位
                    Integer isManualFrozen = stoManage.getIsManualFrozen();
                    if(isManualFrozen == null || !isManualFrozen.equals(1)){
                        throw new Exception("操作失败,只能解冻手动冻结的库位!");
                    }
                    stoManage.setIsManualFrozen(0);
                }
                stoManage.setIsPositionFrozen(isFrozen);
                stoManage.setUpdateBy(userId);
                baseMapper.updateById(stoManage);

                //插入库位冻结解冻历史
                VehicleStockFrozenVO vehicleStockFrozenVO = new VehicleStockFrozenVO();
                vehicleStockFrozenVO.setFrozenReason(vo.getFrozenReason());
                vehicleStockFrozenVO.setRemark(vo.getRemark());
                vehicleStockService.newFrozenHis(isFrozen.toString(),vehicleStockFrozenVO,stoManage.getStorageCode(),1);
            }
        }
    }

    @Override
    public Object findTreeList() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Map map = new HashMap();
        List list = baseMapper.findAll(map);
        TreeConfig config = new TreeConfig("0", "仓库名称", true, "storageId", "storageName", "pid", "hasChild","storageType",true);
        return config.toTreeData(list,false);
    }

    @Override
    public PageInfo detail(String storageId, Integer pageNum, Integer pageSize) {
        PageHelperPlus.startPage(pageNum,pageSize);
        List<StoManageVO> list = baseMapper.detail(storageId);
        PageInfo<StoManageVO> pageInfo = new PageInfo<StoManageVO>(list);
        return pageInfo;
    }

    @Override
    public String queryStroageByCode(String code) {
        return baseMapper.queryStroageByCode(code);
    }

    /**
     * 根据库位代码查询仓库全名
     * @param storageCode
     * @return
     * @throws Exception
     */
    @Override
    public String getStorageFullName(String storageCode) throws Exception {
        return baseMapper.getStorageFullName(storageCode);
    }

    /**
     * 根据仓库代码查询四级仓库名称
     * @param pid
     * @return
     * @throws Exception
     */
    @Override
    public StoManageEditVO getStorageNames(String pid) throws Exception {
        return baseMapper.getStorageNames(pid);
    }

    /**
     * 获取所有下级仓库id
     * @param storageId
     * @return
     * @throws Exception
     */
    @Override
    public List<String> getChildStorageId(String storageId) throws Exception {
        return baseMapper.getChildStorageId(storageId);
    }

    /**
     * 导出库位冻结明细
     * @return
     * @throws Exception
     */
    @Override
    public List<VehicleStockFrozenListVO> exportFrozenHis() throws Exception {
        return baseMapper.exportFrozenHis();
    }
}
